package day7;

public class 岛屿数量 {
	//1: 陆地 0：海洋
	//判断岛屿数量
	public static int ret=0;
	public static int m,n;
	public static int[] dx={1,-1,0,0};
	public static int[] dy={0,0,1,-1};
	public static boolean[][] visit;
	public static void dfs(char[][] grid,int i,int j) {
		visit[i][j]=true;
		for(int k=0;k<4;k++){
			int x=i+dx[k],y=j+dy[k];
			for(x>=0&&x<m&&y>=0&&y<n&&visit[x][y]==false&&grid[x][y]=='1') {
				dfs(grid, x, y);
			}
		}
	}
	public int solve (char[][] grid) {
	// write code here
		ret=0;
		m=grid.length;
		n=grid[0].length;
		visit=new boolean[m][n];
		for(int i=0;i<m;i++){
			for(int j=0;j<n;j++){
				if(grid[i][j]=='1'&&visit[i][j]==false){
					dfs(grid,i,j);
					ret++;
				}
			}
		}
		return ret;
	}
}
